for /f "tokens=* usebackq" %%i IN (`"@for %%i in (.) do echo %%~nxi"`) do (
set ROOT=%%i
)
set PUTTY=d:\PuTTY\putty.exe
set PSCP=d:\PuTTY\pscp.exe
set z7=d:\7-Zip\7z.exe
set VIM=d:\Vim\vim81\vim.exe
set POWERSHELL=d:\PowerShell\7\pwsh.exe

IF "%~1" NEQ "" (
call:%~1 %~2 %~3
) else (
echo Usage:^ %~0^ xxx
)
GOTO:EOF

:merge
   echo "Merging">>nohup.out
   echo %date:/=-%_%time% >>nohup.out

   set tt=%dt:~0,14%
   copy /b assets\xml\*.xml %tt%.xml

   echo "Merged">>nohup.out
   echo %date:/=-%_%time%
GOTO:EOF

:pwd
echo %~dp0
GOTO:EOF

:dos2unix
set file=%1
d:\Vim\vim81\vim.exe -c "set ff=unix" -c "wq" %file%
GOTO:EOF

:: ########################################
:: ## Here's the heredoc processing code ##
:: ########################################
:heredoc <uniqueIDX>
setlocal enabledelayedexpansion
set go=
for /f "delims=" %%A in ('findstr /n "^" "%~f0"') do (
    set "line=%%A" && set "line=!line:*:=!"
    if defined go (if #!line:~1!==#!go::=! (goto :EOF) else echo(!line!)
    if "!line:~0,13!"=="call :heredoc" (
        for /f "tokens=3 delims=>^ " %%i in ("!line!") do (
            if #%%i==#%1 (
                for /f "tokens=2 delims=&" %%I in ("!line!") do (
                    for /f "tokens=2" %%x in ("%%I") do set "go=%%x"
                )
            )
        )
    )
)
goto :EOF

:ps1
C:\Windows\system32\cmd.exe /c "powershell -ExecutionPolicy Bypass -file build.ps1 %~1 %~2 %~3 %~4 %~5
goto:eof

:unjar
jar -xvf xxx.jar
GOTO:EOF

:access
ssh -v -p 80 root@114.116.249.207
GOTO:EOF

:clean
rmdir /s /q out
rmdir /s /q log
call gradlew.bat clean
GOTO:EOF

:pack
call gradlew.bat clean bootJar
GOTO:EOF

:reboot
call mvn clean package

GOTO:EOF

:login
START "" %PUTTY% -ssh %REMOTE_USER%@%REMOTE_IP% -pw %REMOTE_PASSWORD%  
GOTO:EOF

:boot
call:pack
for /f "tokens=* usebackq" %%i IN (`"@for %%i in (.) do echo %%~nxi"`) do (
set ROOT=%%i
)
@REM --spring.config.location=%CD%\application.properties
java^
 -Dfile.encoding=utf-8^
 -Dspring.resources.static-locations=file:%CD%\html\ ^
 -jar build\libs\%ARTIFACT%.jar ^
GOTO:EOF

:daemon
call:run_admin errlvl daemon
if '%errlvl%' NEQ '0' (
GOTO:EOF
)
net user administrator /active:yes
set /P SERVICE_ID=<service.id
net start %SERVICE_ID%
pause
GOTO:EOF


:timestamp
::                 -- %~1: return variable
SETLOCAL
for /f "tokens=2 delims==" %%a in ('wmic OS Get localdatetime /value') do set "dt=%%a"
set i=%dt:~0,4%%dt:~4,2%%dt:~6,2%%dt:~8,2%%dt:~10,2%%dt:~12,2%%RANDOM%
(ENDLOCAL & REM -- RETURN VALUES
    IF "%~1" NEQ "" SET %~1=%i%
)
GOTO:EOF

:time
echo year %date:~0,4% 
ECHO month %date:~5,2% 
ECHO date %date:~8,2% 
echo week %date:~10,6% 
echo hour %time:~0,2% 
echo minute %time:~3,2% 
echo second %time:~6,2% 
echo micro second %time:~9,2%
GOTO:EOF

:telnet
telnet 114.116.249.207 %PORT%
GOTO:EOF

:nopass
   %PSCP% -pw %REMOTE_PASSWORD% %USERPROFILE%\.ssh\xxx.pub %REMOTE_USER%@%REMOTE_IP%:%REMOTE_HOME%
   set TEMP_FILE=%RANDOM%
   (
   echo cd %REMOTE_HOME%
   echo chattr -i %REMOTE_HOME%/.ssh/authorized_keys
   echo chmod +w %REMOTE_HOME%/.ssh/authorized_keys
   echo ls -l %REMOTE_HOME%/.ssh/authorized_keys
   echo cat xxx.pub ^>^> %REMOTE_HOME%/.ssh/authorized_keys
   echo chattr +i %REMOTE_HOME%/.ssh/authorized_keys
   echo exit
   )>%TEMP_FILE%

   %PUTTY% -ssh %REMOTE_USER%@%REMOTE_IP% -pw %REMOTE_PASSWORD% -m %TEMP_FILE%
   del %TEMP_FILE%
GOTO:EOF

:publish
call:pack
@REM ping -n 8 127.0.0.1>nul
set TEMP_FILE=%RANDOM%
(
@REM echo rm -rf %REMOTE_FOLDER%
@REM echo rm -f %REMOTE_FOLDER%
echo mkdir -p %REMOTE_FOLDER%
echo mkdir -p %REMOTE_FOLDER%/libs
echo mkdir -p %REMOTE_FOLDER%/html
echo mkdir -p %REMOTE_FOLDER%/resources
echo mkdir -p %REMOTE_FOLDER%/resources/img/
echo mkdir -p %REMOTE_FOLDER%/resources/file/
echo mkdir -p %REMOTE_FOLDER%/html/img/
echo mkdir -p %REMOTE_FOLDER%/html/file/
)>%TEMP_FILE%

%PUTTY% -ssh %REMOTE_USER%@%REMOTE_IP% -pw %REMOTE_PASSWORD% -m %TEMP_FILE%
del %TEMP_FILE%

for %%i in ("build.sh" "daemon.sh" "src\main\resources\config\%APP_PROP%") do (
   %VIM% -c "set ff=unix" -c "wq" %%i
)

@REM %z7% a -y html.tar html
@REM  "html.tar" 
@REM 
for %%i in ("build\libs\%ARTIFACT%.jar" "build.sh" "daemon.sh" "src\main\resources\config\%APP_PROP%") do (
   %PSCP% -pw %REMOTE_PASSWORD% %%i %REMOTE_USER%@%REMOTE_IP%:%REMOTE_FOLDER%
)

set TEMP_FILE=%RANDOM%
(
echo cd %REMOTE_FOLDER%
echo mv %APP_PROP% application.properties
echo mv %ARTIFACT%.jar libs/
echo find . -name '*.sh' ^|xargs dos2unix
echo find . -name '*.sh' ^|chmod +x
@REM echo tar xf html.tar
@REM echo rm html.tar
@REM echo bash daemon.sh
echo bash build.sh install_service
echo sleep 8
echo disown
echo exit
)>%TEMP_FILE%

%PUTTY% -ssh %REMOTE_USER%@%REMOTE_IP% -pw %REMOTE_PASSWORD% -m %TEMP_FILE%
del %TEMP_FILE%
GOTO:EOF

::--------------------------------------------------------
::-- Function section starts below here
::--------------------------------------------------------
:fibonacci  -- calculate recursively the next Fibonacci number greater or equal to a limit
::       -- %~1: return variable reference and current Fibonacci number
::       -- %~2: previous value
::       -- %~3: limit
SETLOCAL
set /a "Number1=%~1"
set /a "Number2=%~2"
set /a "Limit=%~3"
set /a "NumberN=Number1 + Number2"
if /i %NumberN% LSS %Limit% call:fibonacci NumberN,%Number1%,%Limit%
(ENDLOCAL
    IF "%~1" NEQ "" SET "%~1=%NumberN%"
)
goto:eof
